package PhaseSixModuleone.TheSecondQuestion.code

import scala.io.StdIn
import scala.util.Random

object Second extends App {
  //  自定义游戏玩家
  val AIplays = Map("1" -> "小白马", "2" -> "小红马", "3" -> "小灰马")
  //  石头剪刀布
  val cycles = Map("1" -> "石头", "2" -> "剪刀", "3" -> "布")
  //  玩家
  var playA: User = null
  //  电脑
  var playB: Computer = null
  //  对战次数
  var nums = 0

  //程序主入口
  play


  //  电脑角色
  class Computer(var name:String,var score:Int) {
//    var name = ""
//    var score = 0
    var wins = 0
    var fails = 0
    var ties = 0

    def showfist(): String = {
      var num = Random.nextInt(3) + 1
      println(num)
      println(s"${playB.name}出拳：${cycles(num.toString)}")
      num.toString
    }
  }

  //  玩家角色
  class User(var name:String,var score:Int) {
//    var score = 0
    var wins = 0
    var fails = 0
    var ties = 0

    def showfist(): String = {
      var num = StdIn.readLine()
      if (!cycles.keySet.contains(num)) {
        println("输入不符合规范，默认出布")
        num = "3"
      }
      println(s"你出拳：${cycles(num)}")
      num
    }
  }

  //  游戏初始化入口
  def play: Unit = {
    //    初始化对战页面
    init
    //    开始对战
    println(s"您选择了和${playB.name}对战，要开始么？(y/n)")
    var next = true
    var s = StdIn.readLine()
    do {
      s match {
        case "y" => {
          println("请出拳！（1.石头 2.剪刀 3.布）")
          var ares = playA.showfist()
          println(s"${playB.name}出拳！")
          var bres = playB.showfist()
          //判断结果
          judge(ares, bres)
          nums += 1
          println("是否开启下一轮？（y/n）")
          s = StdIn.readLine()
        }
        case "n" => {
          //  结束对战并显示结果
          next = false
          println("退出游戏！")
          println("-------------------")
          showRes()
        }
        case _ => {
          println("是否开启下一轮？（y/n）")
          s = StdIn.readLine()
        }
      }
    } while (next)
  }

  def judge(ares: String, bres: String): Unit = {
    //    如果相同，平局
    var ties = ares == bres
    if (ties) {
      playA.score += 1
      playA.ties += 1
      playB.score += 1
      playB.ties += 1
      println("和局！下次继续努力")
    } else {
      val win = (ares == "1" && bres == "2") || (ares == "2" && bres == "3") || (ares == "3" && bres == "1")
      if (win) {
        // 玩家赢
        playA.score += 2
        playA.wins += 1
        playB.fails += 1
        println("恭喜！小黑马赢啦！")
      } else {
        // 玩家输
        playB.score += 2
        playB.wins += 1
        playA.fails += 1
        println("抱歉！你输了！")
      }
    }
  }

  //  显示对战结果
  def showRes(): Unit = {
    println(s"${playB.name}\tVS\t${playA.name}")
    println(s"对战次数${nums}次\n")
    println("姓名\t得分\t胜局\t和局\t负局")
    println(s"${playA.name}\t${playA.score}\t${playA.wins}\t${playA.ties}\t${playA.fails}")
    println(s"${playB.name}\t${playB.score}\t${playB.wins}\t${playB.ties}\t${playB.fails}")
  }

  //  游戏入口初始界面
  def init: Unit = {
    println("-----欢迎参加世界杯马粮争霸赛-----")
    println("********************************")
    println("**********猜拳开始！！**********")
    println("********************************\n\n")
    //    用户角色
    playA = new User("小黑马",0)

    //    用户选择对战的电脑角色
    println("请选择你要对战的角色：(1.小白马 2.小红马 3.小灰马)")
    val key = StdIn.readLine()
    playB = new Computer(AIplays(key), 0)
  }
}
